import 'dart:async';

import 'package:flutter/material.dart';
import '../public.dart';
import 'package:flutter/cupertino.dart';

/// 生命周期测试
class TestLife extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LifeDemoState();
  }
}

class LifeDemoState extends State<TestLife>
    with WidgetsBindingObserver, RouteAware {
  Timer timer;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    initTimer();
    print('======================>initState');
  }

  void initTimer() {
    timer = Timer.periodic(Duration(milliseconds: 1000), (t) {
      console('我是定时器$t');
      setState(() {});
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    console(state.toString(), l1: 11111);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    ACGN.routeObserver.subscribe(this, ModalRoute.of(context));
    print('======================>didChangeDependencies');
  }

  @override
  void didUpdateWidget(TestLife oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('======================>didUpdateWidget');
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
    print('didPopNext');
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
    print('didPush');
  }

  /// Called when the current route has been popped off.
  void didPop() {
    print('didPop');
  }

  /// Called when a new route has been pushed, and the current route is no
  /// longer visible.
  void didPushNext() {
    print('didPushNext');
  }

  @override
  Widget build(BuildContext context) {
    print('======================>build');
    return Center(
          child: Column(
        children: [
          // ImagesNetworkComponent(
          //   'http://head.3456mh.com/quanmian/user_head/239/590/212.jpg',
          //   width: 16,
          //   height: 16,
          // ),
          GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Text('退出当前页面')),
          GestureDetector(
            child: new Text('去新路由'),
            onTap: () {
              Navigator.of(context)
                  .push(new MaterialPageRoute(builder: (BuildContext c) {
                return new Text('新路由页面');
              }));
            },
          )
        ],
      ));
  }

  deative() {
    print('======================>deative');
  }

  @override
  void reassemble() {
    super.reassemble();
    print('======================>reassemble');
  }

  @override
  void deactivate() {
    super.deactivate();
    print('======================>deactivate');
    var bool = ModalRoute.of(context).isCurrent;
    print('======================>$bool');
    if (bool) {
      // 监听到页面返回，发请求刷新页面操作
      print('返回NewView页面');
    }
  }

  @override
  void dispose() {
    super.dispose();
    cancelTimer();
    WidgetsBinding.instance.removeObserver(this);
    print('======================>dispose');
  }

  void cancelTimer() {
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }
}
